
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#ifndef IPRIMITIVE_H
#define IPRIMITIVE_H

#include "RTRay.h"
#include "RTMatrix44.h"



namespace RT
{
	//forward declaration
	class RTMaterial;

	class RTIPrimitive
	{
	protected:
		RTMaterial* m_material;

		//world matrix (position of the RTPrimitive in world coordinate)
		RTMatrix44 m_worldMatrix;

		//inverse world matrix
		RTMatrix44 m_inverseWorldMatrix;

		//world view (position of the RTPrimitive in camera coordinate
		RTMatrix44 m_worldViewMatrix;

		//inverse world view matrix
		RTMatrix44 m_inverseWorldViewMatrix;
	public:
		//constructor
		RTIPrimitive()
		{
			m_worldViewMatrix.identity(); 
			m_inverseWorldViewMatrix.identity();
			m_worldMatrix.identity(); 
			m_inverseWorldMatrix.identity();
			m_worldViewMatrix.identity(); 
			m_inverseWorldViewMatrix.identity();
		}

		//destructor
		virtual ~RTIPrimitive(){}

		//check intersection between a ray and the RTPrimitive.
		//If globalCoordinates is true, the ray is in global coordinates. If false, it is in view coordinates.
		virtual bool intersect(const RTRay& ray, F32& dist, bool useGlobalCoordinates) = 0;

		//check if a poI32 is inside the RTPrimitive
		virtual bool isInside(const RTVector3f&)const{return false;};

		//gives the normal in a poI32
		virtual void computeNormal(const RTRay& ray, F32 dist, RTVector3f& normal) const = 0;

		//settors the RTPrimitive's material
		void setMaterial(RTMaterial* material){m_material = material;}

		//set a translate, rotate and scale matrix to set the world matrix
		void setMatrices(const RTMatrix44& T, const RTMatrix44& R, const RTMatrix44& S);

		//set the RTPrimitive's world matrix
		void setWorldMatrix(const RTMatrix44& world);

		//get a poI32er to the RTPrimitive's material
		RTMaterial* getMaterial()const{return m_material;}

		//get the inverse world matrix
		RTMatrix44 getInverseWorldMatrix()const{ return m_inverseWorldMatrix;}

		//get the inverse world view matrix
		RTMatrix44 getInverseWorldViewMatrix()const { return m_inverseWorldViewMatrix;}

		//apply the view matrix to the model
		virtual void applyViewMatrix(const RTMatrix44& view);

		//convert a ray from the view coordinates to the local coordinates of the primtive (where the RTPrimitive is at (0, 0, 0)
		void convertRayToLocalCoordinates(const RTRay& in, RTRay& out)const;

		//convert a ray from global coorindates to local coordinates
		void globalToLocal(const RTRay& in, RTRay& out)const;

		//convert a normal from the local coordinates (where the RTPrimitives is at (0, 0, 0)) to the global coordinates
		void convertNormalToGlobalCoordinates(const RTVector3f& in, RTVector3f& out)const;

		//convert a poI32 in space from global coordinates to the local coordinates of the RTPrimitives
		void convertPointToLocalCoordinates(const RTVector3f& in, RTVector3f& out)const;
	};

}
#endif